# -*- encoding: utf8 -*-
from wtforms import Form, StringField, PasswordField
from wtforms.validators import length, DataRequired, Email, ValidationError

from app.models.user import User


class RegisterForm(Form):

    email = StringField(validators=[DataRequired(), length(8, 64), Email(message='email不符合规范')])

    password = PasswordField(validators=[DataRequired(message='密码不可以为空，请输入密码'), length(6, 32)])

    nickname = StringField(validators=[DataRequired(), length(2, 10, message='昵称至少需要2个字符，最多10个字符')])

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('email已经被注册')

    def validate_nickname(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('昵称已经被注册')


class LoginForm(Form):
    email = StringField(validators=[DataRequired(), length(8, 64), Email(message='email不符合规范')])

    password = PasswordField(validators=[DataRequired(message='密码不可以为空，请输入密码'), length(6, 32)])
